package com.junit;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

public class MyTest {
    private int time;
    public String name;
    MyTest(){

    }
    MyTest(String name){

    }
    public static int test(String msg){
        if(msg == null)return -1;
        return(msg.length());
    }

    public static void main(String[] args) throws Exception {
        Class class1 = MyTest.class;
        System.out.println(class1.getName());
        Class class2 = Class.forName("com.junit.MyTest");
        System.out.println(class1 == class2);
        Constructor[] constructors = class1.getDeclaredConstructors();
        for(Constructor constructor : constructors){
            System.out.println(constructor.getParameterCount());
        }
        Constructor constructor = class1.getDeclaredConstructor(String.class);
        MyTest myTest = (MyTest) constructor.newInstance("String.class");
        System.out.println(myTest);
        Field[] field = class1.getDeclaredFields();
        for(Field field1 : field){
            System.out.println(field1.getName() + "-->" + field1.getType());
        }
        Field field1 = class1.getDeclaredField("name");

        field1.set(myTest, "nihao");
        System.out.println(myTest);
        String name = (String)field1.get(myTest);
    }
    public String toString(){
        return "Name : " + name + " time : " + time;
    }
}
